#include "dialogmealdatamanage.h"
#include "ui_dialogmealdatamanage.h"

DialogMealDataManage::DialogMealDataManage(QSqlDatabase *db, QWidget *parent) :
    QDialog(parent, Qt::Window),
    ui(new Ui::DialogMealDataManage),
     currentId(-1),
    m_db(db)
{
    ui->setupUi(this);

    ui->comboBoxFoodGroup->initialize(db);

    m_model = new QSqlQueryModel(this);
    modelQueryStr = "SELECT rowId, foodName, foodCalorie, foodProtein, foodCh, foodFat, foodCost FROM foods";

    m_model->setQuery(modelQueryStr, *m_db);
    m_model->setHeaderData(0, Qt::Horizontal, "ID");
    m_model->setHeaderData(1, Qt::Horizontal, "Név");
    m_model->setHeaderData(2, Qt::Horizontal, "Energia");
    m_model->setHeaderData(3, Qt::Horizontal, "Fehérje");
    m_model->setHeaderData(4, Qt::Horizontal, "Szénhidrát");
    m_model->setHeaderData(5, Qt::Horizontal, "Zsír");
    m_model->setHeaderData(6, Qt::Horizontal, "Költség");

    ui->tableView->setModel(m_model);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);

    ui->tableView->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);

    m_query = new QSqlQuery(*db);

    this->setWindowFlags(Qt::Window);
}

DialogMealDataManage::~DialogMealDataManage()
{
    delete ui;
}

void DialogMealDataManage::on_pushButtonSave_clicked()
{
    if (currentId != -1) {
        m_query->prepare("UPDATE foods SET foodName = :foodName, "
                         "foodCalorie = :foodCalorie, "
                         "foodProtein = :foodProtein, "
                         "foodCh = :foodCh, "
                         "foodFat =:foodFat, "
                         "foodCost = :foodCost, "
                         "foodType = :foodType, "
                         "foodUnit = :foodUnit "
                         "WHERE rowid = :id");
        m_query->bindValue(":foodName", ui->lineEditName->text());
        m_query->bindValue(":foodCalorie", ui->doubleSpinBoxCalorie->value());
        m_query->bindValue(":foodProtein", ui->doubleSpinBoxProtein->value());
        m_query->bindValue(":foodCh", ui->doubleSpinBoxCh->value());
        m_query->bindValue(":foodFat", ui->doubleSpinBoxFat->value());
        m_query->bindValue(":foodCost", ui->doubleSpinBoxCost->value());
        m_query->bindValue(":foodType", ui->comboBoxFoodGroup->itemData(ui->comboBoxFoodGroup->currentIndex()));
        m_query->bindValue(":foodUnit", ui->lineEditUnit->text());
        m_query->bindValue(":id", currentId);
        if (m_query->exec()) {
            m_model->setQuery(modelQueryStr);
            logMessage(QString("%1 sikerrel módosítva").arg(ui->lineEditName->text()));
        } else {
            logMessage(QString("%1 módosítása sikertelen").arg(ui->lineEditName->text()));
        }
    }
}

void DialogMealDataManage::on_pushButtonAddNew_clicked()
{
    if (currentId != -1) {
        ui->groupBox->setTitle("Adatok");
        ui->lineEditName->setText("");
        ui->lineEditName->setFocus(Qt::TabFocusReason);
        ui->doubleSpinBoxCalorie->setValue(0);
        ui->doubleSpinBoxCh->setValue(0);
        ui->doubleSpinBoxCost->setValue(0);
        ui->doubleSpinBoxFat->setValue(0);
        ui->doubleSpinBoxProtein->setValue(0);
        currentId = -1;
    } else { // újat adunk hozzá
        m_query->prepare("INSERT INTO foods "
                         "(foodName, foodCalorie, foodProtein, foodCh, foodFat, foodCost)"
                         "VALUES (:foodName, :foodCalorie, :foodProtein, :foodCh, :foodFat, :foodCost)");
        m_query->bindValue(":foodName", ui->lineEditName->text());
        m_query->bindValue(":foodCalorie", ui->doubleSpinBoxCalorie->value());
        m_query->bindValue(":foodProtein", ui->doubleSpinBoxProtein->value());
        m_query->bindValue(":foodCh", ui->doubleSpinBoxCh->value());
        m_query->bindValue(":foodFat", ui->doubleSpinBoxFat->value());
        m_query->bindValue(":foodCost", ui->doubleSpinBoxCost->value());
        if (m_query->exec()) {
            m_model->setQuery(modelQueryStr, *m_db);
            currentId = 0;
            logMessage(QString("%1 sikerrel hozzáadva").arg(ui->lineEditName->text()));
            on_pushButtonAddNew_clicked();
        } else {
            logMessage(QString("%1 hozzáadása sikertelen").arg(ui->lineEditName->text()));
        }
    }
}

void DialogMealDataManage::on_tableView_clicked(const QModelIndex &index)
{
    showMealData(m_model->data(m_model->index(index.row(), 0)).toLongLong());
}

void DialogMealDataManage::showMealData(qlonglong id)
{
    m_query->prepare("SELECT rowId, foodName, foodCalorie, foodProtein, foodCh, foodFat, foodCost, foodUnit, foodType FROM foods WHERE rowid = :id");
    m_query->bindValue(":id", id);
    if (m_query->exec()) {
        if (m_query->next()) {
            ui->lineEditName->setText(m_query->value(1).toString());
            ui->doubleSpinBoxCalorie->setValue(m_query->value(2).toDouble());
            ui->doubleSpinBoxProtein->setValue(m_query->value(3).toDouble());
            ui->doubleSpinBoxCh->setValue(m_query->value(4).toDouble());
            ui->doubleSpinBoxFat->setValue(m_query->value(5).toDouble());
            ui->doubleSpinBoxCost->setValue(m_query->value(6).toDouble());
            ui->lineEditUnit->setText(m_query->value(7).toString());
            int index = ui->comboBoxFoodGroup->findData(m_query->value(8).toInt());
            if (index != -1) {
                ui->comboBoxFoodGroup->setCurrentIndex(index);
            } else {
                ui->comboBoxFoodGroup->setCurrentIndex(0);
            }
            currentId = id;
            ui->groupBox->setTitle(QString("Adatok - %1").arg(id));
        }
    }
}

void DialogMealDataManage::logMessage(QString msg)
{
    ui->labelStatus->setText(msg);
}

void DialogMealDataManage::on_pushButton_clicked()
{
    if (currentId != -1)  {
        m_query->prepare("DELETE FROM foods WHERE rowid = :id");
        m_query->bindValue(":id", currentId);
        if (m_query->exec()) {
            logMessage(QString("%1 sikerrel törölve").arg(ui->lineEditName->text()));
            currentId = 0;
            m_model->setQuery(modelQueryStr);
            on_pushButtonAddNew_clicked();
        } else {
            logMessage(QString("%1 törlése sikertelen").arg(ui->lineEditName->text()));
        }
    }
}

void DialogMealDataManage::on_lineEditSearch_textEdited(const QString &arg1)
{
    if (arg1 == "") {
        m_model->setQuery(modelQueryStr, *m_db);
    } else {
        QString query = modelQueryStr + " WHERE foodName LIKE '%" + arg1 + "%'";
        m_model->setQuery(query, *m_db);

    }
}
